import { createStore, combineReducers } from 'redux'
import * as types from './action_types'
/* 
  在多个reducer存在的情况下  执行任意一个dispatch的时候 所有的reducer都会挨个执行

*/
function CountReducer(state, action) {
  // console.log(state, action)
  // state = state || {
  //   // 给state 的初始值  state 就是我们所谓的数据源
  //   count: 100,
  //   name: "珠峰",
  //   age: 12
  // }
  console.log("执行了换count的函数")
  if (!state) return {
    count: 100,
    name: "珠峰",
    age: 12
  }
  switch (action.type) {
    case types.ADD:
      return {
        // 返回的对象是一个新的state
        ...state,
        count: state.count + action.n
      }
    case types.MINUS:
      return {
        ...state,
        count: state.count - action.m
      }
    case types.ADDAGE:
      return {
        ...state,
        age: state.age + action.num
      }
    // case 'changeColor':
    //   return {
    //     ...state,
    //     count: 6666
    //   }
    default:
      return {
        ...state
      }
  }

}
function ColorReducer(state = {
  color: 'red'
}, action) {
  console.log("执行了换颜色的函数")
  if (action.type == types.CHANGE_COLOR) {
    return {
      ...state,
      color: action.col
    }
  } else {
    return {
      ...state
    }
  }
}
let rootReducer = combineReducers({
  qqq: CountReducer, ColorReducer
})
console.log(rootReducer)

let store = createStore(rootReducer)

export default store